package com.i2works.smartluolong.api.controller.index;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.i2works.smartluolong.api.utils.BaseModule;
import com.i2works.smartluolong.utils.base.GlobalConstant;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.utils.plugin.Pager;
import com.i2works.smartluolong.utils.web.HtmlConstant;
import com.i2works.smartluolong.web.entity.Implement.ImplementDO;
import com.i2works.smartluolong.web.entity.Implement.ImplementQuery;
import com.i2works.smartluolong.web.entity.common.SearchParam;
import com.i2works.smartluolong.web.entity.items.ItemsCategoryDO;
import com.i2works.smartluolong.web.entity.items.ItemsCategoryQuery;
import com.i2works.smartluolong.web.entity.items.ItemsDO;
import com.i2works.smartluolong.web.entity.items.ItemsQuery;
import com.i2works.smartluolong.web.service.implement.IImplementService;
import com.i2works.smartluolong.web.service.items.IItemsCategoryService;
import com.i2works.smartluolong.web.service.items.IItemsService;
import com.xiaoleilu.hutool.http.HtmlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 搜索控制器
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/8/26 22:04
 */

@Controller
@RequestMapping
public class SearchController extends BaseModule {

    @Autowired
    private IItemsCategoryService itemsCategoryService;

    @Autowired
    private IItemsService itemsService;

    @Autowired
    private IImplementService implementService;

    /**
     * 搜索
     */
    @PostMapping("/newSearch")
    @ResponseBody
    public Result  search(@ModelAttribute SearchParam searchParam) {
        JSONObject jsonObject=new JSONObject();
        try {
            if (searchParam.getPage() == null || searchParam.getPage() == 0) {
                searchParam.setPage(1);
            }
            Integer pageNumber = searchParam.getPage();
            String keyword = searchParam.getKeyword();
            if (StringUtils.isBlank(keyword)) {
                return R.error("请输入要搜索的信息");
            } else {
                List<ImplementDO> itemsList = Lists.newArrayList();
                ImplementQuery itemsQuery = new ImplementQuery();
                itemsQuery.setStatus("Y");
                itemsQuery.setSearch(keyword);
                Integer queryCount = implementService.queryCount(itemsQuery);
                if (queryCount > 0) {
                    itemsQuery.setOffset(itemsQuery.getLimit() * (pageNumber - 1));
                    itemsList = implementService.queryList(itemsQuery);
                }
                ItemsCategoryQuery cateQuery=new ItemsCategoryQuery();
                cateQuery.setStatus(1);
                List<ItemsCategoryDO> cateList=itemsCategoryService.queryList(cateQuery);
                jsonObject.put("cateList",cateList);
                //项目
                jsonObject.put("itemsList", itemsList);
                //关键词
                jsonObject.put("keyword", HtmlUtil.cleanHtmlTag(keyword));
                //当前页码
                jsonObject.put("pageNumber", pageNumber);
                //分页实体
                jsonObject.put("pager", new Pager(pageNumber, queryCount,10));
                return R.success(jsonObject);
            }
        } catch (Exception e) {
            logger.error("SearchController.search搜索异常：" + e.getMessage());
            return R.error("搜索结果出错！");
        }
    }
    
    
    /**
     * 搜索
     */
    @PostMapping("/search")
    @ResponseBody
    public Result  oldSearch(@ModelAttribute SearchParam searchParam) {
    	Map<String, Object> map=new HashMap<String, Object> ();
        try {
            if (searchParam.getPage() == null || searchParam.getPage() == 0) {
                searchParam.setPage(1);
            }
            Integer pageNumber = searchParam.getPage();
            String keyword = searchParam.getKeyword();
                List<ItemsDO> itemsList = Lists.newArrayList();
                ItemsQuery itemsQuery = new ItemsQuery();
                itemsQuery.setStatus(GlobalConstant.status.TRUE);
                if (StringUtils.isNotBlank(keyword)) {
                	itemsQuery.setSearch(keyword);
                }
                Integer queryCount = itemsService.queryCount(itemsQuery);
                if (queryCount > 0) {
                    itemsQuery.setLimit(Long.valueOf(GlobalConstant.page.PAGE_SIZE));
                    itemsQuery.setOffset(itemsQuery.getLimit() * (pageNumber - 1));
                    itemsList = itemsService.queryList(itemsQuery);
                }
                ItemsCategoryQuery cateQuery=new ItemsCategoryQuery();
                cateQuery.setStatus(1);
                List<ItemsCategoryDO> cateList=itemsCategoryService.queryList(cateQuery);
                map.put("cateList",cateList);
                //项目
                map.put("itemsList", itemsList);
                //当前页码
                map.put("keyword", HtmlUtil.cleanHtmlTag(keyword));
                //每页条数
                map.put("pageNumber", itemsQuery.getLimit() );
       
            return R.success(map);
        } catch (Exception e) {
        	return     R.error();
        }
    }
    
}
